#include "galeria.h"
#include "ui_galeria.h"
#include <QMessageBox>
#include <QtDebug>
#include <QMouseEvent>
#include <QFileInfo>

Galeria::Galeria(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Galeria)
{
    ui->setupUi(this);
    ile=0;

}

Galeria::~Galeria()
{
    delete ui;
}

void Galeria::stworzGalerie()
{
    ile=0;
    vec.clear();
    vecMin.clear();
    vecPixmap.clear();
    vecButton.clear();
    vecLabel.clear();
    QString dirname = QFileDialog::getExistingDirectory(
        this,
        tr("Select a Directory"),
        QDir::currentPath() ); //wybiera katalog
    if( !dirname.isNull() )
    {
      qDebug( dirname.toAscii() );
    }
    currentDir= QDir(dirname.toAscii()); //wybrany katalog
    qDebug() << "QDir Dir:" << currentDir << endl;
    QFileInfoList infos = currentDir.entryInfoList(QDir::Files, QDir::Name); //tylko pliki, alfabetycznie
    foreach(const QFileInfo &info, infos)
    {
        QProcess dcraw;
        qDebug() << "dcraw64" <<"-e"<<info.absoluteFilePath();
        dcraw.start("dcraw64", QStringList() <<"-e" <<info.absoluteFilePath());
        dcraw.waitForFinished();
        QString strOut=dcraw.readAllStandardOutput(); //std wyjscie
        QString strErr=dcraw.readAllStandardError(); // std bledy
        qDebug()<<strOut;
        qDebug()<<strErr;
        if(strErr=="")
        {
            ile++;
            vec.push_back(info.absoluteFilePath().toStdString());
        }
    }
    qDebug()<<ile;
    qDebug()<<vec.size();

    infos =currentDir.entryInfoList(QStringList() << "*.thumb.jpg"<< "*.thumb.ppm", QDir::Files, QDir::Name);//szukanie miniaturek
    foreach(const QFileInfo &info, infos)
    {
        qDebug("%s", qPrintable(info.fileName()));
        vecMin.push_back(info.absoluteFilePath().toStdString());
    }
    qDebug()<<vecMin.size();
    //wyswietlanie miniaturek
    this->setGeometry(0,0,200*ile,100);
    /*QScrollArea *scroll = new QScrollArea(this);
    scroll->setGeometry(0,0,300,120);
    scroll->setWidget(Galeria);*/
    qDebug()<<"przed for-em";
    for(int i=0;i<ile;i++)
    {
        qDebug()<<"poczatek fora "<<i;
        std::string s=vecMin[i]; //sciezka miniatury do stringa
        QString qstr=QString::fromStdString(s); //string do qstringa
        QPixmap *pixmap=new QPixmap(qstr); //tworze pixmap
        vecPixmap.push_back(*pixmap); //dodaje pixmap do wektora
        QLabel *label=new QLabel("",this); //tworze label
        label->setScaledContents(1); //wlaczam skalowanie
        label->setGeometry(i*200,0,200,100); //ustawiam polozenie i rozmiar
        vecLabel.push_back(label); //dodaje label do wektora
        (vecLabel.at(i))->show(); //pokaz label
        (vecLabel.at(i))->setPixmap(vecPixmap.at(i)); //ustawiam pixmap na labelu
        qDebug()<<"koniec fora "<<i;
    }
    qDebug()<<"za for-em";
}

void Galeria::mousePressEvent(QMouseEvent *e)
{ 
    int x,y;
    if (e->button() == Qt::LeftButton)
    {
        x = e->x();
        y = e->y();

        qDebug()<<"hihi slot dziala!";
        qDebug()<<x;
        qDebug()<<y;
    }
    x=x/200;
    qDebug()<<"*"<<x+1;
    QString sciezka=QString::fromStdString(this->vec.at(x));
    QMessageBox::information(this,tr("Plik"),sciezka,QMessageBox::Ok,0);

    //QFile file= new QFile("aaa");
    //QFileInfo fileinfo= new QFileInfo(&strMsg);
    QFileInfo fileinfo(sciezka);
    qDebug()<<fileinfo.baseName();
    emit wyslijSciezke(sciezka);

}
